﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0 - 2DPhysics Demo
 *  ----------------------------------------------------------------------------
 *  File:       GamePlayScreen.cs
 *  Author:     Shane-PC\Shane
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Collisions;

using EuphoricEngine;
using EuphoricEngine.Components.Components2D;

namespace Demo_2DPhysics
{
    public class ScreenBounds : Component
    {
        private PhysicsSprite _topBounds;
        private PhysicsSprite _rightBounds;
        private PhysicsSprite _bottomBounds;
        private PhysicsSprite _leftBounds;

        private PhysicsSimulator _physics;

        public ScreenBounds(PhysicsSimulator physicsSimulator)
            : base()
        {
            _physics = physicsSimulator;
        }

        protected override void Load()
        {
            Body body;
            Geom geom;
            float thickness = 5f;
            float width = (float)Parent.Engine.GraphicsDevice.Viewport.Width;
            float height = (float)Parent.Engine.GraphicsDevice.Viewport.Height;

            // Top Bounds!
            body = BodyFactory.Instance.CreateRectangleBody(_physics, width, thickness, 1f);
            geom = GeomFactory.Instance.CreateRectangleGeom(_physics, body, width, thickness);

            _topBounds = new PhysicsSprite(
                new Vector2(width * .5f, -thickness),
                body,
                geom
            );
            _topBounds.IsStatic = true;
            _topBounds.SetCollisionCategories(CollisionCategory.All);

            // Right Bounds!
            body = BodyFactory.Instance.CreateRectangleBody(_physics, thickness, height, 1f);
            geom = GeomFactory.Instance.CreateRectangleGeom(_physics, body, thickness, height);

            _rightBounds = new PhysicsSprite(
                new Vector2(width + thickness, height * .5f),
                body,
                geom
            );
            _rightBounds.IsStatic = true;
            _rightBounds.SetCollisionCategories(CollisionCategory.All);

            // Bottom Bounds
            body = BodyFactory.Instance.CreateRectangleBody(_physics, width, thickness, 1f);
            geom = GeomFactory.Instance.CreateRectangleGeom(_physics, body, width, thickness);

            Vertices verts = geom.LocalVertices;
            verts.SubDivideEdges(20);
            geom.SetVertices(verts);

            _bottomBounds = new PhysicsSprite(
                new Vector2(width * .5f, height + thickness),
                body,
                geom
            );
            _bottomBounds.IsStatic = true;
            _bottomBounds.SetCollisionCategories(CollisionCategory.All);

            // Left Bounds!
            body = BodyFactory.Instance.CreateRectangleBody(_physics, thickness, height, 1f);
            geom = GeomFactory.Instance.CreateRectangleGeom(_physics, body, thickness, height);

            _leftBounds = new PhysicsSprite(
                new Vector2(-thickness, height * .5f),
                body,
                geom
            );
            _leftBounds.IsStatic = true;
            _leftBounds.SetCollisionCategories(CollisionCategory.All);
        }

        public override void Update()
        {
            _topBounds.Update();
            _rightBounds.Update();
            _bottomBounds.Update();
            _leftBounds.Update();

            base.Update();
        }
    }
}